package cn.elves.model.auth.controller;

import cn.elves.model.auth.entity.dto.ResponseDTO;
import cn.elves.model.auth.entity.dto.RoleDTO;
import cn.elves.model.auth.entity.po.Role;
import cn.elves.model.auth.entity.po.User;
import cn.elves.model.auth.service.RoleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Set;

@RestController
public class RoleController {

    @Resource
    private RoleService roleService;

    @GetMapping("role/page/{pageNum}/{pageSize}")
    public ResponseDTO<Page<Role>> getRolePage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {

        return ResponseDTO.success(roleService.page(new Page<>(pageNum,pageSize)));
    }

    @GetMapping("role/{id}")
    public ResponseDTO<Role> getRoleDetail(@PathVariable("id") Long id) {
        return ResponseDTO.success(roleService.getById(id));
    }

    @PostMapping("role")
    public ResponseDTO<Role> saveRole(@RequestBody RoleDTO RoleDTO) {
        roleService.save(RoleDTO.toRole());
        return ResponseDTO.success();
    }

    @PutMapping("role")
    public ResponseDTO<Role> updateRole(@RequestBody RoleDTO RoleDTO) {
        roleService.updateById(RoleDTO.toRole());
        return ResponseDTO.success();
    }

    @DeleteMapping("role/{id}")
    public ResponseDTO<Role> deleteRoleById(@PathVariable("id") Long id) {
        roleService.removeById(id);
        return ResponseDTO.success();
    }

    @PatchMapping("allocationInterface/{id}")
    public ResponseDTO<User> allocationInterface(@PathVariable("id") Long roleId, @RequestBody Set<Long> interfaceIdSet) {
        roleService.allocationInterface(roleId,interfaceIdSet);
        return ResponseDTO.success();
    }

    @PatchMapping("allocationMenu/{id}")
    public ResponseDTO<User> allocationMenu(@PathVariable("id") Long roleId, @RequestBody Set<Long> menuIdSet) {
        roleService.allocationMenu(roleId,menuIdSet);
        return ResponseDTO.success();
    }

}
